<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表 - 图书借阅系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .book-card {
            transition: transform 0.3s;
            height: 100%;
        }
        .book-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .book-cover {
            height: 200px;
            background-color: #e9ecef;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
        }
        .search-section {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="fas fa-book"></i> 图书借阅系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/book/list">图书列表</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    <c:choose>
                        <c:when test="${not empty sessionScope.reader}">
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/dashboard">个人中心</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/logout">退出登录</a>
                            </li>
                        </c:when>
                        <c:otherwise>
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/login">读者登录</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/register">读者注册</a>
                            </li>
                        </c:otherwise>
                    </c:choose>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <h2>图书列表</h2>
        
        <!-- 搜索区域 -->
        <div class="search-section">
            <form action="/book/search" method="get" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">书名</label>
                    <input type="text" class="form-control" name="title" placeholder="输入书名">
                </div>
                <div class="col-md-4">
                    <label class="form-label">作者</label>
                    <input type="text" class="form-control" name="author" placeholder="输入作者">
                </div>
                <div class="col-md-4">
                    <label class="form-label">分类</label>
                    <select class="form-select" name="categoryId">
                        <option value="">全部分类</option>
                        <option value="1">文学</option>
                        <option value="2">科技</option>
                        <option value="3">历史</option>
                        <option value="4">教育</option>
                        <option value="5">艺术</option>
                    </select>
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search"></i> 搜索
                    </button>
                    <a href="/book/list" class="btn btn-secondary">重置</a>
                </div>
            </form>
        </div>

        <!-- 图书展示 -->
        <div class="row">
            <c:forEach items="${books}" var="book">
                <div class="col-md-3 mb-4">
                    <div class="card book-card">
                        <div class="book-cover">
                            <i class="fas fa-book fa-3x"></i>
                        </div>
                        <div class="card-body">
                            <h5 class="card-title">${book.title}</h5>
                            <p class="card-text text-muted">作者: ${book.author}</p>
                            <p class="card-text">
                                <small class="text-muted">ISBN: ${book.isbn}</small><br>
                                <small class="text-success">可借: ${book.availableQuantity}/${book.totalQuantity}</small>
                            </p>
                            <div class="d-flex justify-content-between">
                                <a href="/book/detail/${book.id}" class="btn btn-primary btn-sm">查看详情</a>
                                <c:if test="${not empty sessionScope.reader and book.availableQuantity > 0}">
                                    <button class="btn btn-success btn-sm" onclick="borrowBook(${book.id})">
                                        <i class="fas fa-hand-holding"></i> 借阅
                                    </button>
                                </c:if>
                            </div>
                        </div>
                    </div>
                </div>
            </c:forEach>
        </div>

        <c:if test="${empty books}">
            <div class="text-center py-5">
                <i class="fas fa-search fa-3x text-muted mb-3"></i>
                <h4 class="text-muted">暂无图书信息</h4>
            </div>
        </c:if>
    </div>

    <!-- 页脚 -->
    <footer class="bg-dark text-light mt-5 py-4">
        <div class="container text-center">
            <p>&copy; 2024 图书借阅系统. 保留所有权利.</p>
        </div>
    </footer>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script>
        function borrowBook(bookId) {
            if (confirm('确定要借阅这本书吗？')) {
                $.post('/borrow/borrow', {bookId: bookId}, function(response) {
                    if (response === '借阅成功') {
                        alert('借阅成功！');
                        location.reload();
                    } else {
                        alert(response);
                    }
                });
            }
        }
    </script>
</body>
</html>